import json
from datetime import datetime
import time
from common.basic import send_message_to_kafka, generate_random_string


data = {
    "packetType": "http",
    "data": {
            "id": "E36E3FE977D042B2970AD75D70E33E04",
            "httpType": 1,
            "host": "10.10.2.233:31100",
            "port": 31100,
            "httpDirection": 2,
            "uri": "/api/pythonTest/gogo",
            "fullUri": "http://10.10.2.233:31100/api/pythonTest/gogo",
            "requestIp": "127.0.0.2",
            "requestMethod": "POST",
            "requestTime": None,
            "requestTimestamp": None,
            "requestTimestampStr": None,
            "requestContentLength": 110,
            "requestContentType": "application/json",
            "requestHeader": "{\"Referer\":\"http://10.10.2.236:31269\",\"Origin\":\"10.10.2.233:31101\",\"User-Agent\":\"POSTMAN\",\"Accept\":\"text/plain, application/json, application/*+json, */*\",\"Connection\":\"Keep-Alive\",\"Host\":\"10.10.2.240:32381\",\"Accept-Encoding\":\"gzip,deflate\",\"Content-Length\":\"110\",\"Content-Type\":\"application/json\"}",
            "requestBody": "{\"name\":\"http://10.10.2.236:31269\",\"@type\":\"200\"}",
            "sensitiveChars": None,
            "statusCode": 200,
            "costTime": 18,
            "responseIp": "10.10.2.240",
            "responseTime": None,
            "responseTimestamp": None,
            "responseTimestampStr": None,
            "responseContentLength": 0,
            "responseContentType": "application/json",
            "responseHeader": "{\"Set-Cookie\":\"name=123123;SameSite=strict\",\"Access-Control-Allow-Origin\":\"*\",\"Transfer-Encoding\":\"chunked\",\"Keep-Alive\":\"timeout=60\",\"Connection\":\"keep-alive\",\"Vary\":\"Access-Control-Request-Headers\",\"Date\":\"Mon, 10 Oct 2022 13:54:38 GMT\",\"Content-Type\":\"application/json\"}",
            "responseBody": "{\"code\":\"200\",\"msg\":\"hello\",\"id\": \"12345\"}}",
            "complete": True,
            "project": "scan_api"
    }
}

current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
current_timestamp = int(time.time() * 1000)

# 替换时间与时间戳
data["data"]["requestTime"] = current_time
data["data"]["requestTimestamp"] = current_timestamp
data["data"]["responseTime"] = current_time
data["data"]["responseTimestamp"] = current_timestamp

# 替换ID
data["data"]["id"] = generate_random_string(32)

# 写入报文txt，读取报文内容

pwd = '../filesOutput/JSON反序列化.txt'

with open(pwd, 'w') as file:
    json.dump(data, file, indent=4)
print("已更新报文")

file = open(pwd, 'r')
file_content = file.read()
file.close()

# 发送消息到Kafka队列
send_message_to_kafka('10.10.1.36:9092', 'api-neta-parser', pwd, data)